/*
 * 
 * Server-Client multiple file downloader by konstantin
 * gabe.konstantin@gmail.com
 * v0.1
 * 
 * TODO: heavy exception handling
 *       swing surface
 * 
 * Changelog: -
 * 
 * Server side
 * 
 */

package serverpatcher;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ServerPatcher {
  public static void main(String[] args) throws IOException {
    
      ServerSocket socket = new ServerSocket(9000);
      
      List<String> fileList = new ArrayList<String>();
        fileList.add("msvcr110.dll");
        fileList.add("root.eix");
        fileList.add("root.epk");
        fileList.add("icon.eix");
        fileList.add("icon.epk");  
      
      while (true) {
          System.out.println("\nWaiting for new connections...");
          for(String item : fileList){ //
          
	  
          Socket client = socket.accept();
          
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(item));

          BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream());
               
          byte[] buffer = new byte[1024 * 50];
          int len = 0;
          while ((len = in.read(buffer)) > 0) {
               out.write(buffer, 0, len);
          }
            System.out.println("[+] File '" + item + "' transfered to client: " + client.getInetAddress().getHostName());
          in.close();
          out.flush();
          out.close();
          
          client.close();
        }
          
      }
      
    }
  }